$(function() {
	upload_file.init();
});

var upload_file = {
		init: function init(){
			this.prepareUploadify();
			this.bindDisplayFile();
		},
		prepareUploadify : function prepareUploadify(){
			var questions = $(".question");
			//console.log(questions);
			for (var i = 0; i < questions.length; i++) {
				var question_id = $(questions[i]).find(".question-id").text();
				var upload_file = $("#uploadify-file-"+question_id);
				if($(questions[i]).hasClass("qt-shortanswer") || $(questions[i]).hasClass("qt-essay") || $(questions[i]).hasClass("qt-analytical")) {
					//console.log(question_id+' : '+upload_file);
					//console.log(location.protocol + "//" + location.host + "/" + 'admin/upload-uploadify/');
					//console.log(document.getElementsByTagName('base')[0].href + 'admin/upload-uploadify/');
					upload_file.uploadify({
						'debug'	 : false,
						'buttonText'	: '点击上传',
						'buttonCursor'	: 'pointer',
						'uploader'	 : document.getElementsByTagName('base')[0].href + 'admin/upload-uploadify/',
						//'uploader'	 : '../../admin/upload-uploadify',
						'swf'	 : document.getElementsByTagName('base')[0].href + 'resources/js/uploadify/uploadify.swf',
						'multi'	 : false,
						'auto'	 : true,
						'height'	 : '26',
						'width'	 : '60',
						'requeueErrors'	: false, //队列上传出错，是否继续回滚队列，即反复尝试上传。默认：false
						'fileSizeLimit'	: '10240', // expects input in kb
						'cancelImage'	: document.getElementsByTagName('base')[0].href + 'resources/js/uploadify/cancel.png',
						overrideEvents:['onSelectError','onDialogClose'],
						onUploadProgress: function() {
							//$('#loader').show();
						},
						onUploadComplete: function(file) {
	//						$('#div-file-list').html('<a id=\'file-name\'>' + file.name + '</a>');
	//						$('#loader').fadeOut(100);
							//alert(file);
							//console.log(file);
							$('#maincontent').load(location.href+' #maincontent > *');
							$(".fade-file").modal('hide');
						},
						onUploadSuccess : function(file, data, response) {
					            console.log('The file ' + file.name + ' was successfully uploaded with a response of ' + response + ':' + data);
					            console.log('data: '+data);
					        	var fileurl = data.split("|")[0];
					        	var filename = data.split("|")[1];
//				        		var textareaval = $(".question-content textarea").val();
//				        		$(".question-content textarea").val( textareaval + "<img class=\"question-content-file\" src=\"" +  fileurl + "\">");
				        		//upload_file.after("<a href=\"..\\" + fileurl + "\" class=\"diaplay-file display-content-file\" target=\"_blank\" data-url=\"" + fileurl + "\">"+filename+"</a>");
				        		var displayFile = upload_file.parent().find(".diaplay-file");
				        		if(displayFile.length == 0){
				        			upload_file.after("<a href=\"..\\" + fileurl + "\" class=\"diaplay-file display-content-file\" target=\"_blank\" data-url=\"" + fileurl + "\">"+filename+"</a>");
				        		}else{
				        			displayFile.attr("href", "..\\"+fileurl);
				        			displayFile.attr("data-url", fileurl);
				        		}
						},
						onSelectError: function(file,errorCode,errorMsg) {
							alert(errorCode+" "+errorMsg);
							if(errorCode==-110){
								util.error("只能上传10M以下的文件。");
								return false;
							}
						},
						onUploadError: function(file,errorCode,errorMsg, errorString) {
							console.log(file);
							console.log(errorCode);
							console.log(errorMsg);
							console.log(errorString);
							util.error(errorMsg);
						}
					});
				}
			}
		},
		
		bindDisplayFile : function bindDisplayFile(){
			$("#bk-conent-question-content").delegate(".diaplay-file","click",function() {
				window.open(location.protocol + "//" + location.host + "/" + $(this).attr("href"));
				e.preventDefault();
			});
		}
		
};